﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DingDingHintAPP
{
    public partial class Form1 : Form
    {
        private TimeSpan ts;
        public Form1()
        {
            InitializeComponent();
        }

        private void Label1_Click(object sender, EventArgs e)
        {
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            OnTimeEvent();
        }
        private void OnTimeEvent()
        {
            var label = this.label1;
            var form = label.FindForm();
            var button = this.button1;

            StringBuilder str = new StringBuilder();
            str.PrintCountDown(ts);
            str.PrintPunchTheClockHintContetn();
            //str.AppendFormSizePrint(form);
            //str.AppendLabelSizePrint(label);
            label.Text = str.ToString();
            label.CenterPositionLocation(form);
            ts = ts.Subtract(new TimeSpan(0, 0, 1));
            if (ts.TotalSeconds < 0.0)
            {
                timer1.Enabled = false;
                button.ButtonShow(form);
            }
        }
        private void CloseFormWindow(Form form)
        {
            form.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            ts = new TimeSpan(0, 1, 0);
            //ts = new TimeSpan(0, 0, 5);
            button1.ButtonHide();
            OnTimeEvent();
        }

        private int MoveCount = 0;
        private bool IsCanCloseWindow = false;
        private void Button1_Click(object sender, EventArgs e)
        {
            var button = this.button1;
            var form = this.FindForm();
            if (MoveCount > 5)
            {
                IsCanCloseWindow = true;
                CloseFormWindow(form);
                return;
            }
            button.ButtonMovePosition(this);
            MoveCount++;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (IsCanCloseWindow)
            {
                return;
            }
            this.label1.Text = $"想企图关闭窗口(参数: {e.CloseReason.ToString()})? 想的太美了, 打卡了没就关窗口!";
            switch (e.CloseReason)
            {
                //应用程序要求关闭窗口
                case CloseReason.ApplicationExitCall:
                    e.Cancel = false; //不拦截，响应操作
                    break;
                //自身窗口上的关闭按钮
                case CloseReason.FormOwnerClosing:
                    e.Cancel = true;//拦截，不响应操作
                    break;
                //MDI窗体关闭事件
                case CloseReason.MdiFormClosing:
                    e.Cancel = true;//拦截，不响应操作
                    break;
                //不明原因的关闭
                case CloseReason.None:
                    break;
                //任务管理器关闭进程
                case CloseReason.TaskManagerClosing:
                    e.Cancel = true;//拦截，不响应操作
                    break;
                //用户通过UI关闭窗口或者通过Alt+F4关闭窗口
                case CloseReason.UserClosing:
                    e.Cancel = true;//拦截，不响应操作
                    break;
                //操作系统准备关机
                case CloseReason.WindowsShutDown:
                    e.Cancel = false;//不拦截，响应操作
                    break;
                default:
                    break;
            }
        }
    }

    public static class FormExtentFunction
    {
        public static void PrintCountDown(this StringBuilder str, TimeSpan ts)
        {
            str.Append("关闭按钮出现倒计时: ");
            str.Append(ts.Hours.ToString().PadLeft(2, '0'));
            str.Append(':');
            str.Append(ts.Minutes.ToString().PadLeft(2, '0'));
            str.Append(':');
            str.AppendLine(ts.Seconds.ToString().PadLeft(2, '0'));
        }
        public static void PrintPunchTheClockHintContetn(this StringBuilder str)
        {
            str.AppendLine($"记得打卡! 打卡! 打卡!");
            str.AppendLine($"快打卡啊! 快打卡啊! 不然扣钱了!");
        }
        public static void AppendFormSizePrint(this StringBuilder str, Form form)
        {
            str.AppendLine($"form.Width:{form.Width}");
            str.AppendLine($"form.Height:{form.Width}");
        }
        public static void AppendLabelSizePrint(this StringBuilder str, System.Windows.Forms.Label label)
        {
            str.AppendLine($"label.Width:{label.Width}");
            str.AppendLine($"label.Height:{label.Height}");
        }
        public static void CenterPositionLocation(this System.Windows.Forms.Label label, Form form)
        {
            label.Location = new System.Drawing.Point((form.Width - label.Width) / 2, (form.Height - label.Height) / 2);
        }
        public static void ButtonShow(this System.Windows.Forms.Button button, Form form)
        {
            button.Enabled = true;
            button.Show();
            button.ButtonMovePosition(form);
        }
        public static void ButtonHide(this System.Windows.Forms.Button button)
        {
            button.Enabled = false;
            button.Hide();
        }
        public static void ButtonMovePosition(this System.Windows.Forms.Button button, Form form)
        {
            int max_x_region = form.Width - button.Width;
            int max_y_region = form.Height - button.Height;
            Random r = new Random();
            int x = r.Next(0, max_x_region);
            int y = r.Next(0, max_y_region);
            button.Location = new System.Drawing.Point(x, y);
        }
    }
}
